/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.propertiesController;

import java.io.File;
import systole.processor.correlation.CorrelationKind;

/**
 *
 * @author jmj
 */
public class SettingsProperties extends SystoleProperties {

    private final static String SETTINGS_FILE_NAME = "general_settings";
    private final static String SETTINGS_PROPERTY = "settings.";
    private String defaultPath = System.getProperty("user.home");

    @Override
    protected void loadProperties() {
        this.properties = this.propertiesReader.loadProperties(SETTINGS_FILE_NAME);
    }

    @Override
    public void saveProperties() {
        this.propertiesReader.saveProperties(properties, SETTINGS_FILE_NAME);
    }

    /**
     * @return default path for files exported
     */
    public String loadExportReportPath() {
        String path = this.loadStringValue(SETTINGS_PROPERTY + "export.path");
        if (path == null) {
            return this.defaultPath;
        }
        try {
            File theDir = new File(path);
            if (!theDir.exists()) {
                return this.defaultPath;
            }
            return path;
        } catch (Exception e) {
            return this.defaultPath;
        }
    }

    /**
     * @param path default path for files exported
     */
    public void saveExportReportPath(String path) {
        this.saveStringValue(SETTINGS_PROPERTY + "export.path", path);
    }

    /**
     * @return default path to import file signals
     */
    public String loadImportSignalPath() {
        String path = this.loadStringValue(SETTINGS_PROPERTY + "import.path");
        if (path == null) {
            return this.defaultPath;
        }
        try {
            File theDir = new File(path);
            if (!theDir.exists()) {
                return this.defaultPath;
            }
            return path;
        } catch (Exception e) {
            return this.defaultPath;
        }
    }

    /**
     *
     * @return max amount of segmets to choose on autocorrelation
     */
    public Integer loadMaxAmountOfSegments() {
        Integer max = this.loadIntValue(SETTINGS_PROPERTY + "correlation.maxAmount");
        if ((max == null) || (max <= 0)) {
            max = 8;
        }
        return max;
    }

    /**
     *
     * @param amount ax amount of segmets to choose on autocorrelation
     */
    public void saveMaxAmountOfSegments(Integer amount) {
        if (amount == null) {
            amount = 8;
        }
        this.saveIntValue(SETTINGS_PROPERTY + "correlation.maxAmount", amount);
    }

    /**
     *
     * @return minimum value of correlation to select a segment
     */
    public Integer loadCorrelationValue() {
        Integer correlation = this.loadIntValue(SETTINGS_PROPERTY + "correlation.value");
        if ((correlation == null) || (correlation < 1) || (correlation > 100)) {
            correlation = 95;
        }
        return correlation;
    }

    /**
     *
     * @param correlation minimum value of correlation to select a segment
     */
    public void saveCorrelationValue(Integer correlation) {
        if (correlation == null) {
            correlation = 95;
        }
        this.saveIntValue(SETTINGS_PROPERTY + "correlation.value", correlation);
    }

    /**
     *
     * @return correlation kind
     */
    public CorrelationKind loadCorrelationKind() {
        return this.loadCorrelationEnum(SETTINGS_PROPERTY + "correlation.kind");
    }

    /**
     *
     * @param kind correlation kind
     */
    public void saveCorrelationKind(CorrelationKind kind) {
        this.saveCorrelationEnum(SETTINGS_PROPERTY + "correlation.kind", (kind == null ? CorrelationKind.SEGMENTO : kind));
    }

    /**
     * @param path default path to import file signals
     */
    public void saveImportSignalPath(String path) {
        this.saveStringValue(SETTINGS_PROPERTY + "import.path", path);
    }
}
